const moment = require('moment'),
      saveLog = require('../utils/fileSystem').saveLog;

const errorHandler = (err, req, res, next) => {
    if(!res.writableEnded) res.status(500).send('Oh no! Something went horribly wrong...');

    const errorMessage = `\n!!! [${moment().format('MMMM DD YYYY @ HH:mm:ss Z')}] AN ERROR OCCURED:\n${err.stack}`;

    console.log(errorMessage);
    saveLog(errorMessage);
};

module.exports = errorHandler;
